<?php
/*
SMF Links
Version 1.4
by:vbgamer45
http://www.smfhacks.com
*/

if (!defined('SMF'))
	die('Hacking attempt...');

function LinksMain()
{

	//Load the main links template
	loadtemplate('Links');
	
	//Load the language files
	if(loadlanguage('Links') == false)
		loadLanguage('Links','english');


	//Link actions
	$subActions = array(

		'addlink' => 'AddLink',
		'addlink2' => 'AddLink2',
		'editlink' => 'EditLink',
		'editlink2' => 'EditLink2',
		'visit' => 'VisitLink',
		'deletelink' => 'DeleteLink',
		'deletelink2' => 'DeleteLink2',
		'catup' => 'CatUp',
		'catdown' => 'CatDown',
		'addcat' => 'AddCat',
		'addcat2' => 'AddCat2',
		'editcat' => 'EditCat',
		'editcat2' => 'EditCat2',
		'deletecat' => 'DeleteCat',
		'deletecat2' => 'DeleteCat2',
		'rate' => 'RateLink',
		'approve' => 'Approve',
		'noapprove' => 'NoApprove',
		'alist' => 'ApproveList',
		'admin' => 'LinksAdmin',
		'admin2' => 'LinksAdmin2',
	);


	// Follow the sa or just go to main links index.
	if (!empty($subActions[@$_GET['sa']]))
		$subActions[$_GET['sa']]();
	else
		view();

}
function view()
{
	global $context, $mbname, $txt, $db_prefix;

	//Check if the current user can view the links list
	isAllowedTo('view_smflinks');

	//Load the main index links template
	$context['sub_template']  = 'main';

	
	
	//Check if there was a category
	if(isset($_REQUEST['cat']))
	{
		$cat = (int) @$_REQUEST['cat'];
		//List all the catagories
		$dbresult = db_query("SELECT title FROM {$db_prefix}links_cat WHERE ID_CAT = $cat LIMIT 1", __FILE__, __LINE__);
		$row = mysql_fetch_assoc($dbresult);
		//Set the page title
		$context['page_title'] = $mbname . $txt['smflinks_title'] . ' - ' . $row['title'];
		
		mysql_free_result($dbresult);
	}
	else 
		$context['page_title'] = $mbname . $txt['smflinks_title'];
}
function AddCat()
{
	global $context, $mbname, $txt;
	isAllowedTo('links_manage_cat');

	$context['sub_template']  = 'addcat';

	//Set the page title
	$context['page_title'] = $mbname . $txt['smflinks_title'] . ' - ' . $txt['smflinks_addcat'];

}
function AddCat2()
{
	global $db_prefix, $txt;
	isAllowedTo('links_manage_cat');

	//Clean the input
	$title = htmlspecialchars($_POST['title'], ENT_QUOTES);
	$description =  htmlspecialchars($_POST['description'], ENT_QUOTES);
	$image =  htmlspecialchars($_POST['image'], ENT_QUOTES);
	
	if($title == '')
		fatal_error($txt['smlinks_nocattitle'],false);

	//Do the order
	$dbresult = db_query("SELECT roworder FROM {$db_prefix}links_cat ORDER BY roworder DESC", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult);

	$order = $row['roworder'];
	$order++;

	//Insert the category
	db_query("INSERT INTO {$db_prefix}links_cat
			(title, description,roworder,image)
		VALUES ('$title', '$description',$order,'$image')", __FILE__, __LINE__);
	mysql_free_result($dbresult);


	redirectexit('action=links');


}
function EditCat()
{
	global $context, $mbname, $txt;
	isAllowedTo('links_manage_cat');

	$context['sub_template']  = 'editcat';

	//Set the page title
	$context['page_title'] = $mbname . $txt['smflinks_title'] . ' - ' . $txt['smflinks_editcat'];

}
function EditCat2()
{
	global $db_prefix, $txt;
	isAllowedTo('links_manage_cat');

	//Clean the input
	$title =  htmlspecialchars($_POST['title'], ENT_QUOTES);
	$description =  htmlspecialchars($_POST['description'], ENT_QUOTES);
	$image =  htmlspecialchars($_POST['image'], ENT_QUOTES);
	$catid = (int) $_REQUEST['catid'];

	if($title == '')
		fatal_error($txt['smlinks_nocattitle'],false);

	//Update the category
	db_query("UPDATE {$db_prefix}links_cat
		SET title = '$title', description = '$description', image = '$image' WHERE ID_CAT = $catid LIMIT 1", __FILE__, __LINE__);



	redirectexit('action=links');

}
function DeleteCat()
{
	global $context, $mbname, $txt;
	isAllowedTo('links_manage_cat');

	$context['sub_template']  = 'deletecat';

	//Set the page title
	$context['page_title'] = $mbname . $txt['smflinks_title'] . ' - ' . $txt['smflinks_deltcat'];

}
function DeleteCat2()
{
	global $db_prefix;
	isAllowedTo('links_manage_cat');
	$catid = (int) $_REQUEST['catid'];

	//Delete All links
	db_query("DELETE FROM {$db_prefix}links WHERE ID_CAT = $catid", __FILE__, __LINE__);
	//Finally delete the category
	db_query("DELETE FROM {$db_prefix}links_cat WHERE ID_CAT = $catid LIMIT 1", __FILE__, __LINE__);

	redirectexit('action=links');
}



function AddLink()
{
	global $context, $mbname, $txt;
	isAllowedTo('add_links');

	$context['sub_template']  = 'addlink';

	//Set the page title
	$context['page_title'] = $mbname  . $txt['smflinks_title'] . ' - ' . $txt['smflinks_addlink'];

}

function AddLink2()
{
	global $ID_MEMBER,$db_prefix,$txt;

	isAllowedTo('add_links');

	//Clean the input
	$title =  htmlspecialchars($_POST['title'], ENT_QUOTES);
	$description = htmlspecialchars($_POST['description'], ENT_QUOTES);
	$url = htmlspecialchars($_POST['url'], ENT_QUOTES);
	$catid = (int) $_REQUEST['catid'];

	if($title == '')
		fatal_error($txt['smflinks_nolinktitle'],false);

	if($url == '')
		fatal_error($txt['smflinks_nolinkurl'],false);

	//Check if the url already exists?
	$dbresult = db_query("SELECT l.url,l.ID_CAT, l.title linkname, c.title cname FROM {$db_prefix}links AS l, {$db_prefix}links_cat AS c WHERE l.url = '$url' AND l.ID_CAT = c.ID_CAT", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult);
	//The link already exists
	if(db_affected_rows() != 0)
	{
		$msg = $txt['smflinks_linkexists'];
		
		$msg = str_replace('%c', $row['cname'], $msg);
		$msg = str_replace('%l', $row['linkname'], $msg);
		
		fatal_error($msg,false);
	
	}	
	mysql_free_result($dbresult);

	//Insert the links
	$t = time();
	if(allowedTo('links_auto_approve'))
	{
		db_query("INSERT INTO {$db_prefix}links
			(ID_CAT, url, title, description,ID_MEMBER,date,approved)
		VALUES ($catid,'$url','$title', '$description',$ID_MEMBER,$t,1)", __FILE__, __LINE__);
	}
	else
	{
		db_query("INSERT INTO {$db_prefix}links
			(ID_CAT, url, title, description,ID_MEMBER,date,approved)
		VALUES ($catid,'$url','$title', '$description',$ID_MEMBER,$t,0)", __FILE__, __LINE__);

		fatal_error($txt['smflinks_linkneedsapproval']);
	}
	//Redirect back to category
	redirectexit('action=links;cat=' . $catid);


}
function EditLink()
{
	global $context, $mbname, $txt;
	isAllowedTo('edit_links');

	$context['sub_template']  = 'editlink';

	//Set the page title
	$context['page_title'] = $mbname  . $txt['smflinks_title'] . ' - ' . $txt['smflinks_editlink'];


}
function EditLink2()
{
	global $db_prefix,$txt;
	isAllowedTo('edit_links');

	//Clean the input
	$title = htmlspecialchars($_POST['title'], ENT_QUOTES);
	$description = htmlspecialchars($_POST['description'], ENT_QUOTES);
	$url = htmlspecialchars($_POST['url'], ENT_QUOTES);
	$catid = (int) $_REQUEST['catid'];
	$id = (int) $_REQUEST['id'];

	if($title == '')
		fatal_error($txt['smflinks_nolinktitle'],false);

	if($url == '')
		fatal_error($txt['smflinks_nolinkurl'],false);

	//Update the category
	db_query("UPDATE {$db_prefix}links
		SET title = '$title',url= '$url', description = '$description', ID_CAT = $catid WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);


	//Redirect back to category
	redirectexit('action=links');

}
function DeleteLink()
{
	global $context, $mbname, $txt;
	isAllowedTo('delete_links');

	$context['sub_template']  = 'deletelink';

	//Set the page title
	$context['page_title'] = $mbname  . $txt['smflinks_title'] . ' - ' . $txt['smflinks_dellink'];

}
function DeleteLink2()
{
	global $db_prefix, $txt;

	isAllowedTo('delete_links');

	$id = (int) @$_REQUEST['id'];

	if(empty($id))
		fatal_error($txt['smflinks_nolinkselected']);

	db_query("DELETE FROM {$db_prefix}links WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);

}
function VisitLink()
{
	global $db_prefix,$txt;
	//Check if the current user can view the links list
	isAllowedTo('view_smflinks');

	$id = (int) @$_REQUEST['id'];

	if(empty($id))
		fatal_error($txt['smflinks_nolinkselected']);

	//Update site lists
	db_query("UPDATE {$db_prefix}links
		SET hits = hits + 1 WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);

	//Redirect to the site
	$dbresult = db_query("SELECT url FROM {$db_prefix}links WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult);
	mysql_free_result($dbresult);
	header("Location: " . $row['url']);
}
function CatUp()
{
	global $db_prefix, $txt;
	//Check if they are allowed to manage cats
	isAllowedTo('links_manage_cat');

	//Get the cat id
	$cat = (int) @$_REQUEST['cat'];
	//Check if there is a category above it
	//First get our row order
	$dbresult1 = db_query("SELECT roworder FROM {$db_prefix}links_cat WHERE ID_CAT = $cat", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult1);
	$oldrow = $row['roworder'];
	$o = $row['roworder'];
	$o--;

	mysql_free_result($dbresult1);
	$dbresult = db_query("SELECT ID_CAT, roworder FROM {$db_prefix}links_cat WHERE roworder = $o", __FILE__, __LINE__);
	if(db_affected_rows()== 0)
		fatal_error($txt['smflinks_nocatabove'],false);
	$row2 = mysql_fetch_assoc($dbresult);


	//Swap the order Id's
	db_query("UPDATE {$db_prefix}links_cat
		SET roworder = $oldrow WHERE ID_CAT = " .$row2['ID_CAT'], __FILE__, __LINE__);

	db_query("UPDATE {$db_prefix}links_cat
		SET roworder = $o WHERE ID_CAT = $cat", __FILE__, __LINE__);


	mysql_free_result($dbresult);

	//Redirect to index to view cats
	redirectexit('action=links');
}
function CatDown()
{
	global $db_prefix, $txt;

	//Check if they are allowed to manage cats
	isAllowedTo('links_manage_cat');

	//Get the cat id
	$cat = (int) @$_REQUEST['cat'];
	//Check if there is a category below it
	//First get our row order
	$dbresult1 = db_query("SELECT roworder FROM {$db_prefix}links_cat WHERE ID_CAT = $cat", __FILE__, __LINE__);
	$row = mysql_fetch_assoc($dbresult1);
	$oldrow = $row['roworder'];
	$o = $row['roworder'];
	$o++;

	mysql_free_result($dbresult1);
	$dbresult = db_query("SELECT ID_CAT, roworder FROM {$db_prefix}links_cat WHERE roworder = $o", __FILE__, __LINE__);
	if(db_affected_rows()== 0)
		fatal_error($txt['smflinks_nocatbelow'],false);
	$row2 = mysql_fetch_assoc($dbresult);


	//Swap the order Id's
	db_query("UPDATE {$db_prefix}links_cat
		SET roworder = $oldrow WHERE ID_CAT = " .$row2['ID_CAT'], __FILE__, __LINE__);

	db_query("UPDATE {$db_prefix}links_cat
		SET roworder = $o WHERE ID_CAT = $cat", __FILE__, __LINE__);


	mysql_free_result($dbresult);


	//Redirect to index to view cats
	redirectexit('action=links');
}
function ApproveList()
{
	global $context, $mbname, $txt;
	isAllowedTo('approve_links');

	$context['sub_template']  = 'approvelinks';

	//Set the page title
	$context['page_title'] = $mbname . $txt['smflinks_title'] .' - ' . $txt['smflinks_approvelinks'] ;
}
function Approve()
{
	global $db_prefix;

	isAllowedTo('approve_links');
	//Get link id
	@$id = (int) $_REQUEST['id'];

	db_query("UPDATE {$db_prefix}links
		SET approved = 1 WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);


	redirectexit('action=links;sa=alist');
}
function NoApprove()
{
	global $db_prefix;

	isAllowedTo('approve_links');
	//Get link id
	@$id = (int) $_REQUEST['id'];

	db_query("UPDATE {$db_prefix}links
		SET approved = 0 WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);


	redirectexit('action=links');
}
function RateLink()
{
	global $db_prefix, $ID_MEMBER, $txt;
	

	isAllowedTo('rate_links');
	
	//Guests can't rate links? Cause how can we keep track???
	is_not_guest();

	//Get the link ID
	@$id = (int) $_REQUEST['id'];

	if(empty($id))
		fatal_error($txt['smflinks_nolinkselected']);

	//See if the user already rated the link.
	$dbresult = db_query("SELECT ID_LINK, ID_MEMBER FROM {$db_prefix}links_rating WHERE ID_MEMBER = $ID_MEMBER AND ID_LINK = $id", __FILE__, __LINE__);
	if(db_affected_rows()!= 0)
		fatal_error($txt['smflinks_alreadyrated'],false);
	mysql_free_result($dbresult);

	//Get the value of rating
	@$value = (int) $_REQUEST['value'];


	//Check value
	if($value == 0)
	{
		//Lower Ranking

		//Insert rating
	db_query("INSERT INTO {$db_prefix}links_rating
			(ID_LINK, ID_MEMBER,value)
		VALUES ($id, $ID_MEMBER,0)", __FILE__, __LINE__);

	//Update main link rating
	db_query("UPDATE {$db_prefix}links
		SET rating = rating - 1 WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);

	}
	else
	{
		//Higher Ranking
		//Insert rating
	db_query("INSERT INTO {$db_prefix}links_rating
			(ID_LINK, ID_MEMBER,value)
		VALUES ($id, $ID_MEMBER,1)", __FILE__, __LINE__);

	//Update main link rating
	db_query("UPDATE {$db_prefix}links
		SET rating = rating + 1 WHERE ID_LINK = $id LIMIT 1", __FILE__, __LINE__);

	}


	redirectexit('action=links');
}
function LinksAdmin()
{
	global $context, $mbname, $txt;
	isAllowedTo('links_manage_cat');


	$context['page_title'] = $mbname . $txt['smflinks_title'] . ' - ' . $txt['smflinks_settings'];

	adminIndex('links_settings');

	$context['sub_template']  = 'manage_cats';

}
function LinksAdmin2()
{
	isAllowedTo('links_manage_cat');
	
	
	$smflinks_setlinksperpage = (int) $_REQUEST['smflinks_setlinksperpage'];
	
	$smflinks_setshowtoprate = isset($_REQUEST['smflinks_setshowtoprate']);
	$smflinks_setshowmostvisited = isset($_REQUEST['smflinks_setshowmostvisited']);
	$smflinks_setshowstats = isset($_REQUEST['smflinks_setshowstats']);
	$smflinks_setallowbbc = isset($_REQUEST['smflinks_setallowbbc']);

	$smflinks_setshowtoprate ? 1 : 0;
    $smflinks_setshowmostvisited ? 1 :  0;
   	$smflinks_setshowstats ? 1 : 0;
    $smflinks_setallowbbc ? 1 : 0;

    //Save the setting information
	updateSettings(
	array('smflinks_setlinksperpage' => $smflinks_setlinksperpage,
	'smflinks_setshowtoprate' => $smflinks_setshowtoprate,
	'smflinks_setshowmostvisited' => $smflinks_setshowmostvisited,
	'smflinks_setshowstats' => $smflinks_setshowstats,
	'smflinks_setallowbbc' => $smflinks_setallowbbc,
	));
	
	
	redirectexit('action=links;sa=admin');
}
?>